package com.xyht.sca_s.student_manage_system.modules.carUseManage.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.entity.req.SmsCarWarehousingReq;
import com.xyht.sca_s.student_manage_system.modules.carUseManage.service.SmsCarWarehousingService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-07-31
 */
@SecretBody
@RestController
@RequestMapping("/smsCarWarehousing")
public class SmsCarWarehousingController {
    @Autowired
    SmsCarWarehousingService smsCarWarehousingService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "车辆批量导入")
    @PostMapping("/batch")
    public ResponseResult batchAddCar(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsCarWarehousingService.batchAddCar(map, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiModelProperty(value = "车辆单个入库")
    @PostMapping("/add")
    public ResponseResult carAdd(@RequestBody SmsCarWarehousingReq warehousingReq,
                                 HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsCarWarehousingService.carAdd(warehousingReq,userId);
    }

    @ApiModelProperty(value = "车辆删除")
    @PostMapping("/delete")
    public ResponseResult carDelete(@RequestBody SmsCarWarehousingReq warehousingReq){
        return smsCarWarehousingService.carDelete(warehousingReq);
    }

    @ApiModelProperty(value = "修改车辆信息")
    @PostMapping("/update")
    public ResponseResult carUpdate(@RequestBody SmsCarWarehousingReq warehousingReq){
        return smsCarWarehousingService.carUpdate(warehousingReq);
    }

    @ApiModelProperty(value = "查询车辆信息")
    @GetMapping("/select")
    public ResponseResult carSelect(@RequestParam(value = "search_str", required = false) String search_str,
                                    @RequestParam(value = "column_name", required = false) String column_name,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "bg_date", required = false) String bg_date,
                                    @RequestParam(value = "end_date", required = false) String end_date){
        return smsCarWarehousingService.carSelect(column_name,search_str,pageSize,pageNum,bg_date,end_date);
    }

    @ApiModelProperty(value = "通过id查询车辆详细信息")
    @GetMapping("/selectById")
    public ResponseResult selectById(@RequestParam(value = "id", required = false) String id
    ){
        return smsCarWarehousingService.selectById(id);
    }

    @ApiModelProperty(value = "查询在库车辆信息")
    @GetMapping("/selectInHouse")
    public ResponseResult carSelectInHouse(
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        return smsCarWarehousingService.carSelectInHouse(pageSize,pageNum);
    }
}

